package com.whlilong.jxc.view;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.part.ViewPart;

import com.whlilong.jxc.constants.IAppConstants;
import com.whlilong.jxc.db.DbManager;
import com.whlilong.jxc.dialog.UserManagerDialog;
import com.whlilong.jxc.input.AcceleratorInput;
import com.whlilong.jxc.sys.CacheImage;
import com.whlilong.jxc.sys.IImageKey;
import com.whlilong.jxc.sys.SaveLogInfo;
import com.whlilong.jxc.util.FileUtil;
import com.whlilong.jxc.util.ViewerChange;

/**
 * 系统设置视图
 * 
 * @author sjy
 * 
 */
public class SystemPropertyView extends ViewPart implements ISelectionListener {

	private CLabel czysz; // 操作员设置
	private CLabel gnqx; // 功能权限
	private CLabel xgmm; // 修改密码
	private CLabel cssz; // 参数设置
	private CLabel about; // 关于软件

	private final Color blue = Display.getCurrent().getSystemColor(
			SWT.COLOR_BLUE);
	private final Color black = Display.getCurrent().getSystemColor(
			SWT.COLOR_BLACK);
	private CLabel initial;
	private CLabel accelerator;
	protected IWorkbenchWindow window = PlatformUI.getWorkbench()
			.getActiveWorkbenchWindow();
	protected Shell shell= null;

	public SystemPropertyView() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub

		createButton(parent);
		PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
				.addSelectionListener(this);
	}

	private void createButton(Composite parent) {
		// TODO Auto-generated method stub
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 4;
		layout.marginTop = 10;
		layout.marginBottom = 10;
		layout.marginRight = 10;
		layout.marginLeft = 10;
		layout.makeColumnsEqualWidth = true;

		composite.setLayout(layout);
		composite.setBackgroundImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.FUNCTIONS_BG));
		composite.setBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		GridData gd = new GridData(GridData.CENTER);
		gd.horizontalIndent = 30;
		gd.verticalIndent = 20;
		gd.widthHint = 120;
		gd.heightHint = 50;

		czysz = new CLabel(composite, SWT.NONE);
		czysz.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1,
				2));
		czysz.setBackground(composite.getBackground());
		czysz.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.ceshi));
		czysz.setText("操作员设置");
		// czysz.setLayoutData(gd);

		gnqx = new CLabel(composite, SWT.NONE);
		gnqx.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1,
				2));
		gnqx.setBackground(composite.getBackground());
		gnqx.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.ceshi));
		gnqx.setText("功能权限");
		// gnqx.setLayoutData(gd);

		xgmm = new CLabel(composite, SWT.NONE);
		xgmm.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1,
				2));
		xgmm.setBackground(composite.getBackground());
		xgmm.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.yuangonginfo));
		xgmm.setText("用户管理");

		accelerator = new CLabel(composite, SWT.NONE);
		accelerator.setBackground(composite.getBackground());
		accelerator.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.acceler));
		accelerator.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 2));
		accelerator.setText("快捷键管理");

		// xgmm.setLayoutData(gd);

		cssz = new CLabel(composite, SWT.NONE);
		cssz.setBackground(composite.getBackground());
		cssz.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.ceshi));
		cssz.setText("参数设置");

		initial = new CLabel(composite, SWT.NONE);
		initial.setBackground(composite.getBackground());
		initial.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.initial));
		initial.setText("开业清仓");
		// cssz.setLayoutData(gd);

		about = new CLabel(composite, SWT.NONE);
		about.setBackground(composite.getBackground());
		about.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.ceshi));
		about.setText("关于软件");
		new Label(composite, SWT.NONE);
		// about.setLayoutData(gd);

		addListener();
	}

	private void addListener() {
		// TODO Auto-generated method stub

		czysz.addMouseTrackListener(new MouseTrackListener() {

			@Override
			public void mouseEnter(MouseEvent e) {
				// TODO Auto-generated method stub
				czysz.setForeground(blue);
			}

			@Override
			public void mouseExit(MouseEvent e) {
				// TODO Auto-generated method stub
				czysz.setForeground(black);
			}

			@Override
			public void mouseHover(MouseEvent e) {
				// TODO Auto-generated method stub
				czysz.setToolTipText(czysz.getText());
			}

		});
		gnqx.addMouseTrackListener(new MouseTrackListener() {

			@Override
			public void mouseEnter(MouseEvent e) {
				// TODO Auto-generated method stub
				gnqx.setForeground(blue);
			}

			@Override
			public void mouseExit(MouseEvent e) {
				// TODO Auto-generated method stub
				gnqx.setForeground(black);
			}

			@Override
			public void mouseHover(MouseEvent e) {
				// TODO Auto-generated method stub
				gnqx.setToolTipText(gnqx.getText());
			}

		});
		xgmm.addMouseTrackListener(new MouseTrackListener() {

			@Override
			public void mouseEnter(MouseEvent e) {
				// TODO Auto-generated method stub
				xgmm.setForeground(blue);
			}

			@Override
			public void mouseExit(MouseEvent e) {
				// TODO Auto-generated method stub
				xgmm.setForeground(black);
			}

			@Override
			public void mouseHover(MouseEvent e) {
				// TODO Auto-generated method stub
				xgmm.setToolTipText(xgmm.getText());
			}

		});
		xgmm.addMouseListener(new MouseAdapter(){

			public void mouseDown(MouseEvent e){
				Shell subshell = new Shell(shell, SWT.DIALOG_TRIM);
				UserManagerDialog ud = new UserManagerDialog(subshell, SaveLogInfo.getInstance().getUsername());
				ud.open();
			}
		});
		cssz.addMouseTrackListener(new MouseTrackListener() {

			@Override
			public void mouseEnter(MouseEvent e) {
				// TODO Auto-generated method stub
				cssz.setForeground(blue);
			}

			@Override
			public void mouseExit(MouseEvent e) {
				// TODO Auto-generated method stub
				cssz.setForeground(black);
			}

			@Override
			public void mouseHover(MouseEvent e) {
				// TODO Auto-generated method stub
				cssz.setToolTipText(cssz.getText());
			}

		});
		initial.addMouseTrackListener(new MouseTrackListener() {

			@Override
			public void mouseEnter(MouseEvent e) {
				// TODO Auto-generated method stub
				initial.setForeground(blue);
			}

			@Override
			public void mouseExit(MouseEvent e) {
				// TODO Auto-generated method stub
				initial.setForeground(black);
			}

			@Override
			public void mouseHover(MouseEvent e) {
				// TODO Auto-generated method stub
				initial.setToolTipText(about.getText());
			}

		});
		initial.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
//				 System.out.println(FileUtil.getWorkspace()+"sql.txt");
				// System.out.println(FileUtil.readTxt(FileUtil.getWorkspace()+"sql.txt",
				// ""));
				if (SaveLogInfo.getInstance().getUsertag().endsWith("0"))
					if (MessageDialog.openConfirm(null, "友情提示",
							"开业清仓将对数据库中的数据进行清除，确定吗？")) {
						DbManager.getInstance().create(
								FileUtil.readTxt(FileUtil.getWorkspace()
										+ "sql.txt", ""));
						MessageDialog.openInformation(null, "清仓成功", "开业清仓成功！");
					} else {
						MessageDialog
								.openInformation(null, "清仓失败", "放弃开业清仓操作！");
					}
				else {
					MessageDialog.openInformation(null, "权限限制", "没有该权限！");
				}
			}

		});
		about.addMouseTrackListener(new MouseTrackListener() {

			@Override
			public void mouseEnter(MouseEvent e) {
				// TODO Auto-generated method stub
				about.setForeground(blue);
			}

			@Override
			public void mouseExit(MouseEvent e) {
				// TODO Auto-generated method stub
				about.setForeground(black);
			}

			@Override
			public void mouseHover(MouseEvent e) {
				// TODO Auto-generated method stub
				about.setToolTipText(about.getText());
			}

		});
		accelerator.addMouseTrackListener(new MouseTrackListener() {

			@Override
			public void mouseEnter(MouseEvent e) {
				// TODO Auto-generated method stub
				accelerator.setForeground(blue);
			}

			@Override
			public void mouseExit(MouseEvent e) {
				// TODO Auto-generated method stub
				accelerator.setForeground(black);
			}

			@Override
			public void mouseHover(MouseEvent e) {
				// TODO Auto-generated method stub
				accelerator.setToolTipText(about.getText());
			}

		});
		accelerator.addMouseListener(new MouseAdapter() {

			public void mouseDown(MouseEvent e) {
				AcceleratorInput input = new AcceleratorInput();
				try {
					window.getActivePage().savePerspective();
					window.getActivePage().close();
					window.openPage(IAppConstants.editor_perspective, null);
					window.getShell().setMaximized(true);
					window.getActivePage().openEditor(input,
							IAppConstants.acceleratorEditor_id, true);
				} catch (PartInitException e1) {
					e1.printStackTrace();
				} catch (WorkbenchException e2) {
					e2.printStackTrace();
				}
			}

		});
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
	}

	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		if (!selection.isEmpty()) {
			IViewPart vp = getViewSite().getPage().findView(
					IAppConstants.systemManagerview_id);

			Table table = ((SystemManagerView) vp).getTableViewer().getTable();
			ViewerChange.getInstance().refreshView(table);
		}
	}

}
